import { Node } from "cc";
import { BaseMgr } from "../base/BaseMgr";
import { ITipsToast } from './ITipsMgr';
import { find } from "cc";
import { Label } from "cc";
import { Graphics } from "cc";
import { UITransform } from "cc";
import { v3 } from "cc";
import { Color } from "cc";
import { tween } from "cc";
import { UIOpacity } from "cc";
import { color } from "cc";
import { getMainWindowSize } from "../../../../@types/packages/scene/@types/cce/utils/window";

export class TipsMgr extends BaseMgr {
	static get inst() {
		return this.getInstance();
	}

	private rootNode: Node = null;

	constructor() {
		super();
		// setInterval(() => {
		//     this.showToast({text: "售卖成功"})
		// }, 1000) 
	}

	public showToast(data: ITipsToast): void {
		const node = this._createNode("Toast");
		const msgType = data.type || 0;
		tween(node)
			.delay(data.times || 0)
			.call(() => {
				const nodeUTF = node.addComponent(UITransform);
				const nodeOpacity = node.addComponent(UIOpacity);
				let parent = this.rootNode;
				let delayTime = 1.5;
				if (data.parent) {
					parent = data.parent;
				}
				if (!parent) {
					parent = find(`Canvas`);
				}
				node.parent = parent;
				node.setSiblingIndex(999);
				if (msgType === 0) {
					node.setPosition(v3(0, 250));
				}
				if (msgType === 1) {
					node.setPosition(v3(-300, -450));
					delayTime = 2;
				}

				const bgNode = this._createNode("bgToast");
				bgNode.parent = node;
				const lpos = nodeUTF.convertToNodeSpaceAR(v3());
				bgNode.setPosition(lpos);
				const bg = bgNode.addComponent(Graphics);
				bg.fillColor = Color.GRAY;

				const animNode = this._createNode("AnimNode");
				animNode.addComponent(UITransform);
				animNode.parent = node;

				const labelNode = this._createNode("lblToast");
				const labelUTF = labelNode.addComponent(UITransform);
				labelNode.parent = animNode;
				const label = labelNode.addComponent(Label);

				if (msgType === 0) {
					label.fontSize = 22;
					label.lineHeight = 22;
					label.color = Color.WHITE;
				}
				if (msgType === 1) {
					label.fontSize = 15;
					label.lineHeight = 15;
					label.color = data.color || Color.WHITE;
				}
				if (msgType === 3) {
					label.fontSize = 18;
					label.lineHeight = 18;
					label.color = data.color || Color.GREEN;
				}
				let text = "";
				text = String(data.text);
				label.string = String(text);
				label.updateRenderData(true);

				let opacityRatio = 0;



				const self = this;
				if (msgType === 3) {
					animNode.setPosition(750, 550)
					tween(animNode)
						.to(15, { position: v3(-400, 550) }, {
							onUpdate(target, ratio) {
								// bg.fillColor = color(50, 50, 50, 100);
								// self._bgFill(bg, labelUTF, msgType);
							},
							onComplete(target){
								animNode.destroy();
								// bg.node.destroy();
							}
						})
						.start();
				} else {
					tween(animNode)
						.by(delayTime, { position: v3(0, 100) }, {
							onUpdate(target, ratio) {
								bg.fillColor = color(50, 50, 50, (1 - opacityRatio) * 255);
								self._bgFill(bg, labelUTF, msgType);
							}
						})
						.start();

					tween(nodeOpacity)
						.delay(delayTime - 0.3)
						.to(0.3, { opacity: 0 }, {
							onUpdate(target, ratio) {
								opacityRatio = ratio;
							},
						})
						.start();
				}
			})
			.start();


	}



	private _bgFill(bg: Graphics, labelUTF: UITransform, msgType: number) {
		let addWidth = 0
		let addHeight = 0
		if (msgType === 0) {
			addWidth = 15
			addHeight = 15 
		}
		if (msgType === 1) {
			addWidth = 10
			addHeight = 10
		}
		if (msgType === 3) {
			addWidth = 10
			addHeight = 10
		}
		const labelNode = labelUTF.node;
		const labelWpos = labelNode.parent.getComponent(UITransform).convertToWorldSpaceAR(labelNode.position);
		// const labelWpos = labelNode.parent.getComponent(UITransform).convertToWorldSpaceAR(labelNode.position);
		const harfWidth = labelUTF.width / 2 + addWidth;
		const harfHeight = labelUTF.height / 2 + addHeight;
		const p1 = v3(labelWpos.x - harfWidth, labelWpos.y - harfHeight);
		const p2 = v3(labelWpos.x + harfWidth, labelWpos.y - harfHeight);
		const p3 = v3(labelWpos.x + harfWidth, labelWpos.y + harfHeight);
		const p4 = v3(labelWpos.x - harfWidth, labelWpos.y + harfHeight);
		bg.clear();
		bg.moveTo(p4.x, p4.y);
		[p1, p2, p3, p4].forEach(p => bg.lineTo(p.x, p.y));
		bg.fill();
	}

	private _createNode(nodeName: string = ""): Node {
		const node = new Node(nodeName);
		node.layer = 1 << 25;
		return node;
	}
}